<template>
	<view class="page">
		<!-- <u-tabs :list="toplist" @click="(i,item)=>topChange(item.id)"></u-tabs> -->
		<view class="tabbox">
			<scroll-view class="scroll" :scroll-with-animation="true" :scroll-x="true" :scroll-left="0">
				<view v-for="(item, i) in toplist" :key="i" class="tab" @click="topChange(item.id)">
					<view :class="topSign==item.id ? 'active' : ''">
						{{item.name }}
					</view>
					<view :class="topSign==item.id ? 'underline' : ''"></view>
				</view>
			</scroll-view>
		</view> 
		<view style="padding: 30rpx;">
			<view class="couponlist">
				<view class="couponbox" v-for="(item,index) in list" :key="index">
					<view class="coupon">
						<view class="coupon-left">
							<view class="price"> {{item.discountPrice || 0}}</view>
							<view class="priceuse"> 满{{item.usePrice}}元可用</view>
						</view>
						<view class="coupon-right">
							<view class="coupontitle"> {{item.name | truncate}}</view>
							<view class="coupontime"> {{item.validEndTime | formatDate}}到期</view>
							<view class="couponbar" >
								余{{item.totalCount-item.takeCount}}张
							</view>
						</view>
						<view class="btn" @click="btnBack(item)" v-if="item.canTake"> 领取 </view>
						<view class="btn" style="background-color: #AAAAAA;" v-else> 已领取 </view>
					</view>
					
				</view>
			</view>
		</view>
		<no-data v-if="list.length==0"></no-data>
		<woodsShareToast ref="woodsShareToast" />
	</view>
</template>

<script>
	import { categoryOneListAPI,couponTakeAPI} from "@/api/appointment.js" 
	import woodsShareToast from "@/components/woods-share-toast.vue"
	import { mapState, mapActions } from "vuex";
	import pageing from "@/mixins/pageing.js"
	import { homecouponAPI } from "@/api/home.js"
	export default {
		mixins: [pageing],
		components: {
			 
			woodsShareToast
		},
		data() {
			return {
				toplist: [],
				topSign: 1,
				list: []
			}
		},
		computed: {
			...mapState("select", ["city"]),
		},
		created() {
			
			this.categoryOneList()
		},

		methods: {
			categoryOneList(){
				categoryOneListAPI().then(res=>{
					this.toplist=res
					this.topSign=res[0].id
					this.getlist()
				})
			},
			showdes(item,index){
				this.list[index].showDescript=!this.list[index].showDescript
				this.$forceUpdate()
				// this.$set(this.list,)
			},
			getlist() {
				homecouponAPI({
					"pageNo": this.pageNo,
					"pageSize": this.pageSize,
					productScopeValue:this.topSign,
					"productScope": 3
				}).then(res => {
					this.total = res.total
					this.list = this.list.concat(res.list.map(item=>{
						return {
							...item,
							showDescript:false
						}
					}));

				})
			},
			topChange(n) {
				this.topSign = n
				this.pageNo=1
				this.list=[]
				this.getlist()
			},
			btnBack(item) {
				couponTakeAPI(2,{templateId:item.id}).then(res=>{
					uni.showToast({
						title:"领取成功"
					})
					this.pageNo=1
					this.list=[]
					this.getlist()
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.page {
		background: #F8F8F8;
		min-height: 100vh;
		padding-top: 80rpx;
		.tabbox {
			position: fixed;
			top: 0px;
			left: 0;
			
			padding: 0 30rpx;
			background-color: #fff;
			z-index: 99;
			 height: 90rpx;
			border-bottom: 1px solid #eef0f4;
			width: 100%;
			.scroll{
				width: 100%;
				display: flex;
				align-items: center;
				height: 100%; // 设置固定高度
				white-space: nowrap; // 防止自动换行
				.tab {
					display: inline-block;
					padding: 20rpx;
					position: relative;
					line-height: 40rpx;
					color: #666;
					text-align: center;
					font-size: 32rpx;
					
				}
				
				.active {
					font-weight: bold;
					color: #333;
					font-size: 32rpx;
				
				}
				
				.underline {
					position: absolute;
					bottom: 0;
					left: 30%;
					width: 40%;
					height: 6rpx;
					background: $uni-color-primary;
					border-radius: 1px;
				}
			}
			

		}

		.coupon {
			background: #fff url('https://jiejinda.oss-cn-beijing.aliyuncs.com/bc23b3dec3aff55f7c83c6620effa30067bb5c4cd48a2e0ce7c8085687548b25.png') 0 0 no-repeat;
			background-size: cover;
			height: 180rpx;
			width: 100%;
			display: flex;
			position: relative;
			border-radius: 20rpx;
			margin-top: 20rpx;

			.coupon-left {
				width: 32%;

				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				.price {
					font-weight: bold;
					font-size: 72rpx;
					color: #FFFFFF;
					line-height: 40rpx;

				}

				.priceuse {
					font-weight: 500;
					font-size: 24rpx;
					color: #FFFFFF;
					margin-top: 15rpx;
					line-height: 40rpx;
				}

			}

			.coupon-right {
				width: 55%;

				.coupontitle {
					font-weight: bold;
					font-size: 30rpx;
					color: #333333;
					text-align: left;
					margin-left: 10rpx;
					line-height: 60rpx;
				}

				.coupontime {
					font-size: 24rpx;
					color: #999999;
					line-height: 58rpx;
					text-align: left;
					font-style: normal;
					text-transform: none;
					margin-left: 18rpx;
				}

				.couponbar {
					font-size: 24rpx;
					display: flex;
					color: #999999;
					line-height: 58rpx;
					margin-left: 12rpx;
				}
			}

			.btn {
				position: absolute;
				right: 12rpx;
				top: 60rpx;
				background: #277EEF;
				border-radius: 34rpx;

				font-size: 24rpx;
				color: #FFFFFF;
				line-height: 40rpx;
				padding: 10rpx 25rpx;
			}
		}

		.descript {
			background-color: #F0F0F0;
			font-size: 20rpx;
			color: #666;
			padding: 16rpx 24rpx;
			border-radius: 0 0 20rpx 20rpx;
			line-height: 40rpx;
		}

	}
</style>